package com.ctshk.app.user.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.user.dto.UserReceivingAddressDTO;
import com.ctshk.rpc.user.dto.UserReceivingAddressListDTO;
import com.ctshk.rpc.user.req.UserReceivingAddressPageReq;
import com.ctshk.rpc.user.req.UserReceivingAddressReq;
import com.ctshk.rpc.user.service.IUserReceivingAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * <p>
 *  用户收货地址前端控制器
 * </p>
 *
 * @author 李新科
 * @date 2021/5/17 17:06
 */
@Api(value="用户收货地址", tags="用户收货地址")
@RestController
@RequestMapping("/userReceivingAddress")
public class UserReceivingAddressController extends AppBaseController {

    @DubboReference
    private IUserReceivingAddressService userReceivingAddressService;

    @ApiOperation(value="收货地址列表", notes="收货地址列表")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "收货地址列表", operDesc = "收货地址列表")
    @PostMapping("/list")
    public Result<PageResponse<UserReceivingAddressListDTO>> queryPage(@RequestBody @Valid UserReceivingAddressPageReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        return userReceivingAddressService.queryPage(req);
    }

    @ApiOperation(value="新增收货地址", notes="新增收货地址")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.ADD, operTitle = "新增收货地址", operDesc = "新增收货地址")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid UserReceivingAddressReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        return userReceivingAddressService.add(req);
    }

    @ApiOperation(value="修改收货地址", notes="修改收货地址")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.UPDATE, operTitle = "修改收货地址", operDesc = "修改收货地址")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid UserReceivingAddressReq req) throws Exception{
        TokenUser currentUser = getCurrentUser();
        req.setUserId(currentUser.getId());
        return userReceivingAddressService.update(req);
    }

    @ApiOperation(value="删除收货地址", notes="删除收货地址")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.DELETE, operTitle = "删除收货地址", operDesc = "删除收货地址")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable @NotNull(message = "id不能为空")
                         @ApiParam(name="id",required = true) Long id) throws Exception{
        TokenUser currentUser = getCurrentUser();
        return userReceivingAddressService.delete(id,currentUser.getId());
    }

    @ApiOperation(value="收货地址详情", notes="收货地址详情")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.QUERY, operTitle = "收货地址详情", operDesc = "收货地址详情")
    @PostMapping("/detail/{id}")
    public Result<UserReceivingAddressDTO> detail(@PathVariable @NotNull(message = "id不能为空")
                                       @ApiParam(name="id",required = true) Long id) throws Exception{
        TokenUser currentUser = getCurrentUser();
        return userReceivingAddressService.detail(id,currentUser.getId());
    }

    @ApiOperation(value="设为默认地址", notes="设为默认地址")
    @OperationLogAnnotation(operModul = OperationLogModel.USER, operType = OperationLogType.UPDATE, operTitle = "设为默认地址", operDesc = "设为默认地址")
    @PostMapping("/setDefaultAddress/{id}/{isDefault}")
    public Result setDefaultAddress(@PathVariable @NotNull(message = "id不能为空")
                                        @ApiParam(name="id",required = true) Long id,
                                    @PathVariable
                                    @Min(value = 0,message = "非法参数")
                                    @Max(value = 1,message = "非法参数")
                                    @ApiParam(name="是否默认,0否1是",required = true) Integer isDefault) throws Exception{
        TokenUser currentUser = getCurrentUser();
        return userReceivingAddressService.setDefaultAddress(id,currentUser.getId(),isDefault);
    }
}
